<?php
namespace App\Models\PartnerModels;
use App\Models\ParentModel;
use DB;
/**
 * api用户表model
 * @author puppet
 */
class ApiUser extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_api_user';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_api_user';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;

    //云快视方法 获取对应账号和type=2的设备id相关信息
    public function getnameInfo($uname){
        $sql="select * from $this->table where user_name=? and status=1 and type=2";
        $re=DB::select($sql,[$uname]);
        return isset($re[0])? $re[0]:'';
    }
    // 该账户名是否可用
    public function checkUname($uname){
        $resout=array();
        if(!empty($uname)){
            $sql="select user_name from $this->table where user_name=? and status!=-1";
            // echo $sql;exit();
            $resout=DB::select($sql,[$uname]);
        }
        return !empty($resout) ? $resout[0] : false;
    }
//    public function checkuip($uname){
//            $sql=" select ip from $this->table where  user_name=? and status!=-1";
//           $resout=DB::select($sql,[$uname]);
//
//          return !empty($resout) ? $resout[0] : false;
//    }
    //验证账号能不能使用
    public function checkApiUname($uname,$pwd,$time,$type){
        if(!empty($uname)) {
            $sql = "select id,user_name,equipment_id,api_token,mac,type from $this->table where user_name=? and pwd='$pwd' and end_date>'$time' and status!=-1 and type=$type";
           //  echo $sql;exit();
            $resout = DB::select($sql, [$uname]);
        }
        return !empty($resout) ? $resout[0] : false;
    }
    //拉取会员详情
    function viewVipInfo($id){// 这是是默认admin创建 所以是左查询的ptadmin表
        $sql="select a.*,b.admin_name from $this->table as a left join pt_admin as b on a.fid =b.admin_id where a.id=? ";
        $re=DB::select($sql,[$id]);
        $re=isset($re[0]['id']) ? $re[0] : false;
        //print_r($re);exit();
        $html="";
        if($re){
            $html.="<p>昵称：{$re['nickname']}</p>";
            $html.="<p>手机号：{$re['mobile']}</p>";
            $gender=$re['gender']==0 ? "未知" : ($re['gender']==1 ? "男" : "女");
            $html.="<p>性别：{$gender}</p>";
            $gtype=$re['gender']==0 ? "未知" : ($re['gender']==1 ? "好友推" : "其他app");
            $html.="<p>类型：{$gtype}</p>";
            $html.="<p>账号创建者：{$re['admin_name']}</p>";
            $html.="<p>创建时间：{$re['create_time']}</p>";
            $html.="<p>修改时间：{$re['update_time']}</p>";
        }
        return $html;
    }
	/**
	* 翻页展示所有信息    移动端列表
	*/
	public function getMobilepPageData($params){
		$res = false;
		$size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
		$page = isset($params['page']) ? intval($params['page']) : 1;
		$offset = ($page - 1) * $size;
        $countSql="SELECT count(*) as count FROM {$this->db_table} WHERE `status` >= 1";
        $sql="select id,pname,type,user_name,nickname,gender,mobile,start_date,end_date,create_time,update_time,status,api_token from $this->table where status >=0 and type=1 ";

		if(isset($params['user_name']) && !empty($params['user_name'])){
			$countSql .= " AND user_name like '%{$params['user_name']}%'";
			$sql .= " AND user_name like '%{$params['user_name']}%'";
		}
		if(isset($params['nickname']) && !empty($params['nickname'])){
			$countSql .= " AND nickname like '%{$params['nickname']}%'";
			$sql .= " AND nickname like '%{$params['nickname']}%'";
		}
		if(isset($params['mobile']) && !empty($params['mobile'])){
			$countSql .= " AND mobile like '%{$params['mobile']}%'";
			$sql .= " AND mobile like '%{$params['mobile']}%'";
		}
		if(isset($params['order']) && !empty($params['order'])){
			$order_db = ['id'];
			$sql .= " ORDER BY ";
			foreach ($params['order'] as $order){
				$result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
				!empty($result) && $sql .=  $result.' '.$order['dir'].',';
			}
			$sql = substr($sql,0,-1);
		}
		$count = DB::select($countSql);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$sql .= " LIMIT $offset,$size";
		$data = DB::select($sql);
		if(!empty($data)){
			$i=0;
			foreach($data as $k=>$v){
				++$i;
				$data[$k]['kid']=$i;
				$data[$k]['gender']=$v['gender']==1 ? '男' : ($v['gender']==2 ? '女' : '未知');
                $data[$k]['type']=$v['type']==1 ? '好友推' : ($v['type']==2 ? '云快视' : '未知');
				$data[$k]['date']=$v['start_date']."——".$v['end_date'];
				$data[$k]['status']='';
				if($v['status']==0){
					$data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"default\" checked onclick=\"actionList({$v['id']},1)\"><span></span></label></div>";
				}elseif($v['status']==1){
					$data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"checkbox\" checked onclick=\"actionList({$v['id']},0)\"><span></span></label></div>";
				}
				$data[$k]['operation']="<a href='javascript:;' title='查看' aria-label='View' data-pjax='0'><span class='glyphicon glyphicon-eye-open' onclick=\"viewVip({$v['id']},'{$v['user_name']}')\"></span></a>";
				$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				$data[$k]['operation'].="<a href='/admin/mobile/edit/{$v['id']}' title='修改' aria-label='Update' data-pjax='0'><span class='glyphicon glyphicon-pencil'></span></a>";
				$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				$data[$k]['operation'].="<a href='javascript:;' title='删除' aria-label='Delete' data-pjax='0' onclick='actionList({$v['id']},-1)'><span class='glyphicon glyphicon-trash'></span></a>";
		        }
		}
		// echo "<pre/>";print_r($data);exit;
		$res['data'] = $data;
		$res['total'] = $count;
		//总条数记录
		return $res;
	}

	//会员套餐 添加、修改
	function saveTaocan($data,$id){
		if($id){
			$sql="update $this->table set ";
			foreach($data as $k=>$v){
				$sql.="`".$k."`='".$v."',";
			}
			$sql=substr($sql,0,-1)." where id={$id}";
			DB::update($sql);
		}else{
			DB::table("$this->table")->insertGetId($data);
		}
	}
	




}